/**
 * 
 */
package com.tvt.sexyapp_demo.downloader;

import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.content.Context;
import android.net.ConnectivityManager;

import com.tvt.sexyapp_demo.debug.Debug;

/**
 * @author MR_HIEU
 * 
 */
public class GNetworkUtils {

	// Constant
	private final static String LOG_TAG = "GNetworkUtils";
	private final static int TIMEOUT_CONNECTION = 10000;
	private final static int TIMEOUT_SOCKET_CONNECTION = 300000;

	public static boolean isWifiAvailable(Context pContext) {

		if (pContext == null) {
			Debug.e(LOG_TAG, "context is null");
			return false;
		}

		boolean wifiState = false;
		final ConnectivityManager connMgr = (ConnectivityManager) pContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		final android.net.NetworkInfo wifi = connMgr
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

		final android.net.NetworkInfo mobile = connMgr
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

		if (wifi.isAvailable()) {
			wifiState = true;
		} else if (mobile.isConnected()) {
			wifiState = true;
		}
		return wifiState;
	}

	public static InputStream doExcuteByGetMethod(String url) {

		Debug.i(LOG_TAG, "Starting to request URL: " + url);

		try {
			HttpParams p = new BasicHttpParams();

			// Set the timeout in milliseconds until a connection is
			// established.
			HttpConnectionParams.setConnectionTimeout(p, TIMEOUT_CONNECTION);

			// Set the default socket timeout (SO_TIMEOUT)
			// in milliseconds which is the timeout for waiting for data.
			HttpConnectionParams.setSoTimeout(p, TIMEOUT_SOCKET_CONNECTION);

			// Instantiate an HttpClient
			HttpClient client = new DefaultHttpClient(p);

			HttpResponse response = client.execute(new HttpGet(url));

			if (response != null) {

				if (response.getStatusLine() != null
						&& response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
					return null;

				if (response.getEntity() != null) {
					return response.getEntity().getContent();
				} else {
					return null;
				}
			} else
				return null;

		} catch (ConnectTimeoutException e) {

			Debug.w(LOG_TAG, "ConnectTimeoutException");
			return null;

		} catch (SocketTimeoutException e) {

			Debug.w(LOG_TAG, "SocketTimeoutException");
			return null;

		} catch (ClientProtocolException e) {

			Debug.w(LOG_TAG, "ClientProtocolException");
			return null;

		} catch (IOException e) {

			Debug.w(LOG_TAG, "IOException");
			return null;

		}
	}

	public static InputStream doExcuteByGetWithCheckingWifi(Context pContext,
			String pUrl) {

		if (!isWifiAvailable(pContext)) {
			Debug.e(LOG_TAG, "Wifi isn't available!");
			return null;
		}

		return doExcuteByGetMethod(pUrl);
	}
}
